import { computed } from 'vue';
import { useDigital } from '../../../utils/useDigital';

const { setAmount } = useDigital();

export const useComputed = props => {
  /**
   * @description 计算数据数组
   * @return {Array} dataArray
   */
  const getData = computed(() => {
    return props.data;
  });

  /**
   * @description 计算列插槽数组
   * @return {Array} slotsArray
   */
  const getColumsSlots = computed(() => {
    let slotColumns = [];
    const slotRec = arr => {
      if (!arr) return;
      arr.map(e => {
        if (e.slot) {
          slotColumns.push(e);
        }
        slotRec(e.children);
      });
      return slotColumns;
    };
    slotRec(props.columns);
    return slotColumns;
  });
  /**
   * @description 计算列数组
   * @return {Array} columns
   */
  const getViewColumns = computed(() => {
    let columns = Array.from(props.columns);
    const columnsRec = obj => {
      if (obj.type === 'amount') {
        obj.render = setAmount;
      }
      if (obj.children) {
        obj.children = obj.children.map(item => columnsRec(item));
      }
      return obj;
    };
    columns = columns.map(item => {
      item = columnsRec(item);
      return item;
    });
    return columns;
  });

  /**
   * @description 计算bind对象
   * @param {Object} props参数对象
   * @return {Object} attrsObject
   */
  const getBindValues = computed(() => {
    return attrs => {
      let propsData = {
        ...attrs,
        columns: getViewColumns.value,
      };
      return propsData;
    };
  });

  return {
    getData,
    getColumsSlots,
    getViewColumns,
    getBindValues,
  };
};
